function indexs() {
    location.href = '../pages/banner.html'
}
let pageNo = 1
let allStr = '' //所有商品列表标签数据

function search() {
    location.href='../pages/search.html?keyword='+ $('input[type="text"]').val()
   
}
function showUser(){
    //获取数据
    let productList= localStorage.getItem('USER')
    let user = JSON.parse(productList)
  
    if(user){
    
     let headerSt=` <p onclick="register()">${user.username}</p>
     <img src="${user.headerimg}" alt="">
    `
      $('.right').html(headerSt)
    }else{
 $('right').html(`<p onclick="register()">注册/登录</p>`)
    }
 }
 
 //退出
 function logout() {
     $('.right>button').on('click', function () {
       
         // 1. 清除localStorage登录用户信息
         localStorage.removeItem('USER')
           $('.right>img').remove()
           $('.right>button').remove()
         // 2. 跳转到登录界面
        
       
         location.href = '../pages/index.html'
       
     })
   
    
 }
function loadProduct(pageNo) {
    ajax({
        method: 'get',
        url: 'http://api.yuguoxy.com/api/shop/list/category',
       
        success: function (data) {
            console.log('<<<<', data);

            let {
                resultCode,
                resultInfo
            } = data //解构赋值
            if (resultCode == 1) {
                let list = resultInfo.list
                console.log(list);
                showproductlist(list)
                // alert('成功')
            } else {
                alert('失败')
            }

        }
    })
}

function showproductlist(list) {
    let str = list.map(item => {
        return ` <div onclick="onDetail(${item.id})">
        <img src="${item.picture}" alt="">
        <p>${item.shop}</p>
        <span>淘宝</span><span>包邮</span>
        <ul>
            <li class="color">￥${item.price}</li>
            
        </ul>
    </div>`
    }).join('')
    allStr += str
    let contents = document.querySelector('.contents')
    contents.innerHTML = allStr
}

function onMst() {
    let buttom = document.querySelector('.musm')
    buttom.addEventListener('click', function () {
        loadProduct(++pageNo)
    })

}

function onDetail(id) {
    location.href = '../pages/banner.html?id=' + id
}

function onsWiper() {
    new Swiper('.swiper', {
        autoplay: true, //可选选项，自动滑动
        loop: true, // 循环模式选项
        // 如果需要分页器
        pagination: {
            el: '.swiper-pagination',
            clickable: true,
        },
        // 如果需要前进后退按钮
        navigation: {
            nextEl: '.swiper-button-next',
            prevEl: '.swiper-button-prev',
        },
    })

}

function loadBanner() {
    ajax({
        method: 'get',
        url: 'http://api.yuguoxy.com/api/shop/banner',
        success: function (data) {
            console.log('<<<<', data);

            let {
                resultCode,resultInfo } = data //解构赋值
            if (resultCode == 1) {
                let list = resultInfo.list
                console.log('//////',list);
                showBanner(list)
                // alert('成功')
            } else {
                alert('失败')
            }

        }
    })
}
function showBanner(list){
    let str=list.map(item=>{
        return `<div class="swiper-slide" ><img src="${item.url}" alt="${item.content}"></div> `
    }).join('')
    let swiperWrapper=document.querySelector('.swiper-wrapper')
    swiperWrapper.innerHTML=str
    onsWiper()
}
function showlist(){
    ajax({
        method: 'get',
        url: 'http://api.yuguoxy.com/api/shop/category/list',
        
        success: function (data) {
            console.log('<<<<', data);

            let {
                resultCode,resultInfo } = data //解构赋值
            if (resultCode == 1) {
                let list = resultInfo.list
                console.log('//////',list);
                showlistt(list)
               
                // alert('成功')
            } else {
                alert('失败')
            }

        }
    })
}


function showlistt(list) {
    let str = list.map(item => {
        return `<li>${item.name}</li>`
    }).join('')
    
    let ull = document.querySelector('.ull>ul')
    ull.innerHTML = str 
}

$('.ul').on('click', 'li', function () {
    $('ul li').removeClass('active')
    $(this).addClass('active')
})
function register(){
    
    location.href='../pages/index1.html'
}
function index1(){
  
    location.href='../pages/index.html'
}
function index2(){
    
    location.href='../pages/index11.html'
}
function index3(){
   
    location.href='../pages/index12.html'
}


loadProduct(pageNo)

loadBanner()
showlist()
showUser()
logout()
